<?php

define("BASE_PATH", realpath((dirname(__FILE__))));  //网站的根目录BASE_PATH="/var/www/SmartyMVC"
define("PACKAGE_PATH" , BASE_PATH."/com/coolking");      //PACKAGE_PATH="/var/www/SmartyMVC/com/coolking"
$projectName = "myproject";
require_once (PACKAGE_PATH.'/Smarty-3.1.12/libs/Smarty.class.php');
$smarty = new Smarty;
$smarty->setTemplateDir(PACKAGE_PATH."/$projectName/templates"); //it's html templates path
$smarty->setCompileDir(PACKAGE_PATH."/$projectName/templates_c");
$smarty->debugging = false;
$smarty->caching = false;
$smarty->cache_lifetime = 0;


session_start();
ob_start();
date_default_timezone_set("Asia/Chongqing");

//web访问的URL地址：http://localhost/SmartyMVC/index.php/action/PageAction/index?language=en
//app访问的URL地址：http://localhost/SmartyMVC/index.php/api/PageAction/index

$uri = $_SERVER["REQUEST_URI"] ; 

if( strpos($uri , "action" ) && strpos($uri , "js" ) == false ){
	// $uri="/SmartyMVC/index.php/action/PageAction/index?mod=111"
	$actionUri = strstr($uri,"action") ;  //$actionUri="/PageAction/index?mod=111"
	$arr = explode("/" , $actionUri  ) ;  //$arr={"","PageAction","index?mod=111"}
	$actionName   =  $arr[1];    //$actionName="PageAction"
	$functionName =  $arr[2];    //$functionName="index?mod=111"
	if( strpos($functionName, "?")  ){
		$functionName = explode("?" , $functionName  ) ; //$functionName={"index","mod=111"}
		$functionName = $functionName[0] ;     //$functionName="index"
	}
	//TODO登录验证处理。。。
    //$actionName="PageAction"
	$folder = explode("Action" , $actionName  )  ;   //$folder={"Page",""}
	$folderName = strtolower($folder[0]);            //$folderName="page"

	require_once(PACKAGE_PATH."/$projectName/action/".$actionName.".php");//require_once(/var/www/SmartyMVC/com/coolking/myproject/action/PageAction.php);
	
	$action = new $actionName ;  //$action=new PageAction
	$action->smarty = $smarty ;   //
	if(in_array( $functionName , get_class_methods($action))){//in_array判断PageAction类中存在index方法，存在才调用该方法
		$html = $action->$functionName();   //$html=index()
		if( $html ){
			$smarty->display( $folderName."/".$html );
		}
	} else{
		$smarty->display( "page/error.html" );
	}
} else if( strpos($uri , "api" ) && strpos($uri , "js" ) == false ){//为Android/ios提供api接口返回json对象
	//$uri="/SmartyMVC/index.php/api/PageAction/index"
	$actionUri = strstr($uri,"api") ;    //$actionUri="/PageAction/index?mod=111"
	$arr = explode("/" , $actionUri  ) ; //$arr={"","PageAction","index?mod=111"}
	$actionName   =  $arr[1];            //$actionName="PageAction"
	$functionName =  $arr[2];            //$functionName="index?mod=111"

	$folder = explode("Action" , $actionName  )  ; //$folder={"Page",""}
	$folderName = strtolower($folder[0]);          //$folderName="page"

	require_once(PACKAGE_PATH."/$projectName/api/".$actionName.".php");
	if( strpos($functionName, "?")  ){//$functionName="index?mod=111"
		$functionName = explode("?" , $functionName  ) ;//$functionName={"index","mod=111"}
		$functionName = $functionName[0] ;//$functionName="index"
	}

	$action = new $actionName ;// new PageAction;
	$action->$functionName();  //$action->index(); 调用api的方法echo出json对象
}
ob_end_flush();
?>
